﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.DataAccess.DO
{
    public partial class TransactionHistoryDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static TransactionHistoryDO GetModelByPK(Session sess, int iInternalID)
        {
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE INTERNAL_ID = @INTENRAL_ID ";
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_ID", iInternalID)});
                if (DataManager.IsEmpty(dtModel))
                    return null;
                return GetModel(dtModel.Rows[0]);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "INV_ITransactionHistory";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM TRANSACTION_HISTORY WHERE INTERNAL_ID = @INTENRAL_ID ";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_ID", this.INTERNAL_ID)});
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "INV_UTransactionHistory";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象ITEM的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<TransactionHistoryDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, SelectSequel);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性ITEM对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<TransactionHistoryDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 从DataView装载持久性ITEM对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<TransactionHistoryDO> LoadListFromDataView(DataView dv)
        {
            List<TransactionHistoryDO> list = new List<TransactionHistoryDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static TransactionHistoryDO GetModel(DataRow row)
        {
            TransactionHistoryDO itemDO = new TransactionHistoryDO();
            if (!row["INTERNAL_ID"].Equals(DBNull.Value)) itemDO.INTERNAL_ID = Convert.ToInt32(row["INTERNAL_ID"]);
            if (!row["TRANSACTION_TYPE"].Equals(DBNull.Value)) itemDO.TRANSACTION_TYPE = Convert.ToString(row["TRANSACTION_TYPE"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) itemDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["ZONE"].Equals(DBNull.Value)) itemDO.ZONE = Convert.ToString(row["ZONE"]);
            if (!row["LOCATION"].Equals(DBNull.Value)) itemDO.LOCATION = Convert.ToString(row["LOCATION"]);
            if (!row["LPN"].Equals(DBNull.Value)) itemDO.LPN = Convert.ToString(row["LPN"]);
            if (!row["ITEM"].Equals(DBNull.Value)) itemDO.ITEM = Convert.ToString(row["ITEM"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) itemDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["ITEM_DESC"].Equals(DBNull.Value)) itemDO.ITEM_DESC = Convert.ToString(row["ITEM_DESC"]);
            if (!row["ATTRIBUTE_NUM"].Equals(DBNull.Value)) itemDO.ATTRIBUTE_NUM = Convert.ToInt32(row["ATTRIBUTE_NUM"]);
            if (!row["QUANTITY"].Equals(DBNull.Value)) itemDO.QUANTITY = Convert.ToInt32(row["QUANTITY"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) itemDO.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["TASK_ID"].Equals(DBNull.Value)) itemDO.TASK_ID = Convert.ToString(row["TASK_ID"]);
            if (!row["TASK_TYPE"].Equals(DBNull.Value)) itemDO.TASK_TYPE = Convert.ToString(row["TASK_TYPE"]);
            if (!row["REFERENCE_ID"].Equals(DBNull.Value)) itemDO.REFERENCE_ID = Convert.ToString(row["REFERENCE_ID"]);
            if (!row["REFERENCE_NUM"].Equals(DBNull.Value)) itemDO.REFERENCE_NUM = Convert.ToInt32(row["REFERENCE_NUM"]);
            if (!row["REFERENCE_NUM_TYPE"].Equals(DBNull.Value)) itemDO.REFERENCE_NUM_TYPE = Convert.ToString(row["REFERENCE_NUM_TYPE"]);
            if (!row["REFERENCE_LINE_NUM"].Equals(DBNull.Value)) itemDO.REFERENCE_LINE_NUM = Convert.ToInt32(row["REFERENCE_LINE_NUM"]);
            if (!row["BEFORE_ON_HAND_QTY"].Equals(DBNull.Value)) itemDO.BEFORE_ON_HAND_QTY = Convert.ToInt32(row["BEFORE_ON_HAND_QTY"]);
            if (!row["AFTER_ON_HAND_QTY"].Equals(DBNull.Value)) itemDO.AFTER_ON_HAND_QTY = Convert.ToInt32(row["AFTER_ON_HAND_QTY"]);
            if (!row["BEFORE_STS"].Equals(DBNull.Value)) itemDO.BEFORE_STS = Convert.ToString(row["BEFORE_STS"]);
            if (!row["AFTER_STS"].Equals(DBNull.Value)) itemDO.AFTER_STS = Convert.ToString(row["AFTER_STS"]);
            if (!row["DIRECTION"].Equals(DBNull.Value)) itemDO.DIRECTION = Convert.ToString(row["DIRECTION"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) itemDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) itemDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);



            return itemDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [TRANSACTION_HISTORY] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(TransactionHistoryDO obj)
        {
            SqlParameter[] paras = new SqlParameter[25];
            paras[0] = new SqlParameter("@INTERNAL_ID", obj.INTERNAL_ID);
            paras[0].DbType = DbType.String;
            paras[1] = new SqlParameter("@TRANSACTION_TYPE", obj.TRANSACTION_TYPE == String.Empty ? DBNull.Value : (object)obj.TRANSACTION_TYPE);
            paras[1].DbType = DbType.String;
            paras[2] = new SqlParameter("@WAREHOUSE", obj.WAREHOUSE == String.Empty ? DBNull.Value : (object)obj.WAREHOUSE);
            paras[2].DbType = DbType.String;
            paras[3] = new SqlParameter("@ZONE", obj.ZONE == String.Empty ? DBNull.Value : (object)obj.ZONE);
            paras[3].DbType = DbType.String;
            paras[4] = new SqlParameter("@LOCATION", obj.LOCATION == String.Empty ? DBNull.Value : (object)obj.LOCATION);
            paras[4].DbType = DbType.String;
            paras[5] = new SqlParameter("@LPN", obj.LPN == String.Empty ? DBNull.Value : (object)obj.LPN);
            paras[5].DbType = DbType.String;
            paras[6] = new SqlParameter("@ITEM", obj.ITEM == String.Empty ? DBNull.Value : (object)obj.ITEM);
            paras[6].DbType = DbType.String;
            paras[7] = new SqlParameter("@ITEM_DESC", obj.ITEM_DESC == String.Empty ? DBNull.Value : (object)obj.ITEM_DESC);
            paras[7].DbType = DbType.String;
            paras[8] = new SqlParameter("@COMPANY", obj.COMPANY == String.Empty ? DBNull.Value : (object)obj.COMPANY);
            paras[8].DbType = DbType.String;
            paras[9] = new SqlParameter("@ATTRIBUTE_NUM", obj.ATTRIBUTE_NUM);
            paras[9].DbType = DbType.String;
            paras[10] = new SqlParameter("@QUANTITY", obj.QUANTITY );
            paras[10].DbType = DbType.String;
            paras[11] = new SqlParameter("@QUANTITY_UM", obj.QUANTITY_UM == String.Empty ? DBNull.Value : (object)obj.QUANTITY_UM);
            paras[11].DbType = DbType.String;
            paras[12] = new SqlParameter("@TASK_ID", obj.TASK_ID == String.Empty ? DBNull.Value : (object)obj.TASK_ID);
            paras[12].DbType = DbType.String;
            paras[13] = new SqlParameter("@TASK_TYPE", obj.TASK_TYPE == String.Empty ? DBNull.Value : (object)obj.TASK_TYPE);
            paras[13].DbType = DbType.String;
            paras[14] = new SqlParameter("@REFERENCE_ID", obj.REFERENCE_ID == String.Empty ? DBNull.Value : (object)obj.REFERENCE_ID);
            paras[14].DbType = DbType.String;
            paras[15] = new SqlParameter("@REFERENCE_NUM", obj.REFERENCE_NUM);
            paras[15].DbType = DbType.String;
            paras[16] = new SqlParameter("@REFERENCE_NUM_TYPE", obj.REFERENCE_NUM_TYPE == String.Empty ? DBNull.Value : (object)obj.REFERENCE_NUM_TYPE);
            paras[16].DbType = DbType.String;
            paras[17] = new SqlParameter("@REFERENCE_LINE_NUM", obj.REFERENCE_LINE_NUM);
            paras[17].DbType = DbType.String;
            paras[18] = new SqlParameter("@BEFORE_ON_HAND_QTY", obj.BEFORE_ON_HAND_QTY);
            paras[18].DbType = DbType.String;
            paras[19] = new SqlParameter("@AFTER_ON_HAND_QTY", obj.AFTER_ON_HAND_QTY);
            paras[19].DbType = DbType.String;
            paras[20] = new SqlParameter("@BEFORE_STS", obj.BEFORE_STS == String.Empty ? DBNull.Value : (object)obj.BEFORE_STS);
            paras[20].DbType = DbType.String;
            paras[21] = new SqlParameter("@AFTER_STS", obj.AFTER_STS == String.Empty ? DBNull.Value : (object)obj.AFTER_STS);
            paras[21].DbType = DbType.String;
            paras[22] = new SqlParameter("@DIRECTION", obj.DIRECTION == String.Empty ? DBNull.Value : (object)obj.DIRECTION);
            paras[22].DbType = DbType.String;
            paras[23] = new SqlParameter("@USER_STAMP", obj.USER_STAMP == String.Empty ? DBNull.Value : (object)obj.USER_STAMP);
            paras[23].DbType = DbType.String;
            paras[24] = new SqlParameter("@DATE_TIME_STAMP", System.DateTime.Now);
            paras[24].DbType = DbType.DateTime;


            return paras;
        }

        private static IDataParameter[] UpdateWherePara(TransactionHistoryDO itemDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_ID", itemDO.INTERNAL_ID);
            return paras;
        }

        #endregion


        public static List<TransactionHistoryDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
